package com.dongge.Test;

import com.dongge.Entity.Student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;

/**
 * Created by zhudong on 2016/10/7.
 */
public class TestMybatisSimple {

    private SqlSession session;

    @Before
    public void init(){
        String resource = "conf-simple.xml";
        //使用类加载器加载mybatis的配置文件（它也加载关联的映射文件）
        InputStream is = getClass().getClassLoader().getResourceAsStream(resource);
        //构建sqlSession的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        //使用MyBatis提供的Resources类加载mybatis的配置文件（它也加载关联的映射文件）
        //Reader reader = Resources.getResourceAsReader(resource);
        //构建sqlSession的工厂
        //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        //创建能执行映射文件中sql的sqlSession
        session = sessionFactory.openSession();
    }

    @Test
    public void testGetStudents(){
        String statement = "com.dongge.Entity.studentMapper.getStudent";
        Student student = session.selectOne(statement,1);
        System.out.println(student);
    }

    @Test
    public void testGetStudentResultMap(){
        String statement = "com.dongge.Entity.studentMapper.getStudentResultMap";
        Student student = session.selectOne(statement,2);
        System.out.println(student);
    }
}
